package com.javacasino.casino.controller;

import com.javacasino.casino.entity.login.LoginInfo;
import com.javacasino.casino.entity.login.Master;
import com.javacasino.casino.service.WebSocketService;
import com.javacasino.casino.service.impl.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Controller
@RequestMapping(value = "/casino")
public class index {

    @Autowired
    private WebSocketService webSocketService;
    @Autowired
    private RedisService redisService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/index.html";
    }

    @RequestMapping(value = "/config", method = RequestMethod.GET)
    public String config() {
        return "/config.html";
    }

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public boolean login(@RequestBody LoginInfo e) {
        boolean login = webSocketService.login(e);
        if (login) {
//            redisService.getCacheObject("login_");
            return true;
        }
            return false;
    }

    /**
     * 对监控号或主号控制是否可下单。  主号开始受监控影响，监控号直接下单
     *
     * @param sid
     * @param flag 1 开始下单
     */
    @ResponseBody
    @RequestMapping(value = "/startOrStop", method = RequestMethod.GET)
    public boolean stop(@RequestParam("sid") String sid,
                     @RequestParam("money") String money,
                     @RequestParam(value = "note",defaultValue = "1") String note,
                     @RequestParam("interval_one") String interval_one,
                     @RequestParam("interval_two") String interval_two,
                     @RequestParam("flag") String flag) {
        boolean b = false;
        if ("1".equals(flag)) {
            b = true;
        }
        Master master = redisService.getCacheObject("master_sid_info:"+sid);
        master.setNote(note);
        master.setMoney(money);
        master.setInterval_one(interval_one);
        master.setInterval_two(interval_two);
        redisService.setCacheObject("master_sid_info:"+sid,master);
        redisService.setCacheObject("bet:" + sid, b);
        return true;
    }

    /**
     * 监控号下单启动事件
     *
     * @param sid
     * @param second
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/bet", method = RequestMethod.GET)
    public boolean bet(@RequestParam("sid") String sid,
                       @RequestParam("second") int second,
                        @RequestParam("is_bet") boolean is_bet) {
        try {
            webSocketService.slavePlaceOrder(sid, second * 1000,is_bet);
        } catch (Exception e) {
            log.error("下单错误", e);
            return false;
        }
        return true;
    }

    /**
     * 获取单号
     * @param sid
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/loadOrderNum",method = RequestMethod.GET)
    public String loadOrderNum(@RequestParam("sid") String sid){
        String orderNum = webSocketService.loadOrderNum(sid);

       return orderNum;
    }

}
